# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : ms_event.py
# Time       ：2021/8/31 15:34
# Author     ：王洪磊
# Email      ： wang_hl007@163.com
# Description： 微震事件类
"""

import os
from utils.auto_arrive_time import auto_pat, auto_pot, auto_sat, auto_sot, wave_energy
from utils.read_wav_raw import read_wave_data


class MsEvent(object):

    TIME_STAMP_NAME = ['pot', 'pat', 'sot', 'sat']

    def __init__(self, parent, name, time, file_path, auto_timestamp: dict, timestamp: dict,
                 use_channel, pos, energy):
        """

        :param name: 微震名字
        :param time: 微震发生时间
        :param parent: 创建者
        :param file_path: 微震事件文件路径
        :param auto_timestamp: 自动标波结果，关键字['pot', 'pat', 'sot', 'sat']不为空的字典
        :param timestamp: 手动标波结果，关键字['pot', 'pat', 'sot', 'sat']可以为空的字典
        :param pos: 微震事件位置， 可以为空字典
        :param energy: 微震事件能量
        :param use_channel: 使用的微震传感器名字，不为空
        """

        self.name = name
        self.time = time
        self.parent = parent
        self.path = file_path
        self.use_channel = use_channel
        self.wave_data = None

        self.auto_timestamp = auto_timestamp
        self.timestamp = timestamp
        # self.auto_pat, self.auto_pot = auto_timestamp.get('pat'), auto_timestamp.get('pot')
        # self.auto_sat, self.auto_sot = auto_timestamp.get('sat'), auto_timestamp.get('sot')
        # self.pat, self.pot = timestamp.get('pat'), timestamp.get('pot')
        # self.sat, self.sot = timestamp.get('sat'), timestamp.get('sot')

        self.pos = pos    # 微震事件的定位关键字为x y z。
        self.energy = energy

    def get_wave_data(self):
        # 如果wave_data是None 返回原始波形，否则返回wave data
        raw_wave = read_wave_data(self.path)
        wave_data = raw_wave[self.use_channel]

        return wave_data

    def set_use_channel(self, channels):
        # 设置使用的通道，如果验证通过,则重置use_channel和wave_data

        self.use_channel = channels

    def get_auto_timestamp(self, name):
        # 获取自动标波的时间，name必须是['pot','pat','sot','sat']中的一个，否则返回空
        # 如果是None 通过自动定位算法获取
        if name in self.TIME_STAMP_NAME:
            timestamp = self.auto_timestamp.get(name)
            return timestamp

        # timestamp = None
        # if name == 'pot':
        #     timestamp = self.auto_pot
        # elif name == 'pat':
        #     timestamp = self.auto_pat
        # elif name == 'sot':
        #     timestamp = self.auto_sot
        # elif name == 'sat':
        #     timestamp = self.auto_sat
        #
        # return timestamp

    def get_energy(self):
        if self.energy is None:
            self.energy = wave_energy(self.wave_data)

        return self.energy

    def set_pos(self, pos):
        self.pos = pos

    def set_timestamp(self, name, timestamp):
        # 设置手动标波的时间，name必须是['pot','pat','sot','sat']中的一个，否则返回空
        if name in self.TIME_STAMP_NAME:
            self.timestamp[name] = timestamp
        # if name == 'pot':
        #     self.pot = timestamp
        # elif name == 'pat':
        #     self.pat = timestamp
        # elif name == 'sot':
        #     self.sot = timestamp
        # elif name == 'sat':
        #     self.sat = timestamp

    def get_timestamp(self, name):
        # 获取手动标波的时间，name必须是['pot','pat','sot','sat']中的一个，否则返回空
        if name in self.TIME_STAMP_NAME:
            timestamp = self.timestamp.get(name)
            return timestamp
        # timestamp = None
        # if name == 'pot':
        #     timestamp = self.pot
        # elif name == 'pat':
        #     timestamp = self.pat
        # elif name == 'sot':
        #     timestamp = self.sot
        # elif name == 'sat':
        #     timestamp = self.sat


def path2name(path):
    folder, file_name = os.path.split(path)

    name = file_name.split('.')[0]
    return name


def test():
    path = r'D:\云空间\程序\0831微震分析系统\resource\sos波形\A01_180803_140014465.bin'
    name = path2name(path)
    parent = []
    auto_timestamp = dict(pot=0.1, pat=1.2, sot=1, sat=4)
    timestamp = dict(pot=0.3, pat=1.1, sot=2, sat=1.5)
    use_channel = ['t1', 't3']
    ms_event = MsEvent(name=name, parent=parent, file_path=path, use_channel=use_channel,
                       auto_timestamp=auto_timestamp, timestamp=timestamp)

    pass


if __name__ == '__main__':
    test()


